package hellfire.platform.input;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.EditText;
import hellfire.platform.screens.AbstractGameScreen;
import hellfire.system.R;
import hellfire.system.engine.Main;
import hellfire.system.graphics.GLColor;
import hellfire.system.graphics.GLDrawable;
import hellfire.system.graphics.GLString;
import hellfire.system.graphics.GLVec2;

public class LineInputField extends GLButton implements GLDrawable
{
	private GLString string;
	private GLColor selected, deselected;
	private GLVec2 pos, dim;
	
	public LineInputField(AbstractGameScreen screen, GLVec2 pos, GLVec2 dim) 
	{
		this(screen, "", pos, dim);
		this.pos = pos;
		this.dim = dim;
	}
	
	public LineInputField(AbstractGameScreen screen, String defaultValue, GLVec2 pos, GLVec2 dim) 
	{
		super(screen, pos, dim, R.drawable.field, R.drawable.field, null);
		super.setHandler(new Event());
		string = new GLString(R.drawable.basefont, defaultValue, pos.sub(dim.x()-0.08f, 0), 0.06f);
		string.setTextColor(GLColor.WHITE);
		selected = GLColor.WHITE;
		deselected = GLColor.GREY;
		getImage().setMaskColor(deselected);
		this.pos = pos;
		this.dim = dim;
	}

	@Override
	public boolean testPosition(GLVec2 pos) 
	{
		return super.testPosition(pos);
		
	}
	
	public void setColors(GLColor sel, GLColor desel)
	{
		this.selected = sel;
		this.deselected = desel;
		if(down) getImage().setMaskColor(selected);
		else getImage().setMaskColor(deselected);
	}
	
	@Override
	public void onTouchEvent(MotionEvent e, GLVec2 pos)
	{
		super.onTouchEvent(e, pos);
		if(down) image.setMaskColor(selected);
		else image.setMaskColor(deselected);
	}
	
	public class Event implements Runnable
	{
		public void run() 
		{
			showInputDialog();
		}
		
		private void showInputDialog()
		{
			AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(Main.Activity);
			dialogbuilder.setTitle("Enter Value");
	    	final EditText input = new EditText(Main.Activity);
	    	dialogbuilder.setView(input);
	    	input.setText(string.getString());
	    	dialogbuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
	    	{
		    	public void onClick(DialogInterface dialog, int whichButton) 
		    	{
		    		String value = input.getText().toString();
		    		string.setString(value, true);
		    	}
		    });
	    	dialogbuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
	    	{
	    	  public void onClick(DialogInterface dialog, int whichButton) 
	    	  {
	    	   
	    	  }
	    	});
	    	dialogbuilder.create().show();
		}
	}

	@Override
	public void draw(float[] matrix)
	{
		super.draw(matrix);
		string.draw(matrix);
		
	}
	
	@Override
	public void setPos(GLVec2 pos)
	{
		super.setPos(pos);
		string.setStartPos(pos.sub(dim.x()-0.08f, 0));
	}
}
